﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThuVienTNN
{
    public class CauHinhGrid
    {
        public static void CauHinh
            (
                DevExpress.XtraGrid.Views.Grid.GridView gridView,
                bool editable = false,
                bool readOnly = false,
                bool allowCellMerge = false,
                bool allowGroup = false,
                bool allowFiler = false,
                bool allowSort = false,
                bool allowColumnMoving = false,
                bool alloColumnResizing = true,
                bool allowQuickHideColumns = false,
                bool showIndicator = false,
                bool showColumnHeaders = true,
                bool showDetailButtons = false,
                bool useEmbeddedNavigator = false,
                bool autoFocusNewRow = true
            )
        {
            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                gridView.Columns[i].AppearanceCell.Options.UseTextOptions = true;
                gridView.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

                gridView.Columns[i].AppearanceHeader.Options.UseTextOptions = true;
                gridView.Columns[i].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }
            //readOnly
            if (readOnly == true)
            {
                gridView.OptionsBehavior.ReadOnly = true;
            }
            else
            {
                gridView.OptionsBehavior.Editable = editable;
            }
            //group column
            gridView.OptionsCustomization.AllowGroup = allowGroup;
            gridView.OptionsCustomization.AllowFilter = allowFiler;
            gridView.OptionsCustomization.AllowSort = allowSort;
            //merge Column
            //------------------
            gridView.OptionsView.AllowCellMerge = allowCellMerge;
            gridView.OptionsMenu.EnableColumnMenu = false;
            gridView.OptionsCustomization.AllowColumnMoving = allowColumnMoving;
            gridView.OptionsCustomization.AllowColumnResizing = alloColumnResizing;
            gridView.OptionsCustomization.AllowQuickHideColumns = allowQuickHideColumns;
            //------------------
            gridView.OptionsView.AllowHtmlDrawHeaders = true;
            gridView.OptionsView.ShowGroupPanel = allowGroup;
            gridView.OptionsView.ShowAutoFilterRow = allowFiler;
            gridView.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;

            gridView.OptionsView.ShowIndicator = showIndicator;
            gridView.OptionsView.ShowColumnHeaders = showColumnHeaders;
            gridView.OptionsView.ShowDetailButtons = showDetailButtons;

            gridView.GridControl.UseEmbeddedNavigator = useEmbeddedNavigator;

            gridView.GridControl.UseEmbeddedNavigator = useEmbeddedNavigator;

            gridView.OptionsNavigation.AutoFocusNewRow = autoFocusNewRow;
            gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

            gridView.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
            gridView.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.True;

            gridView.Appearance.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
        }
        public static void HinhDangCot
            (
            DevExpress.XtraGrid.Views.Grid.GridView gridView,
            DevExpress.XtraGrid.Columns.GridColumn[] columns,
            DevExpress.Utils.HorzAlignment hAlignment = DevExpress.Utils.HorzAlignment.Near
            )
        {
            gridView.BeginUpdate();
            foreach (var column in columns)
            {
                column.AppearanceCell.Options.UseTextOptions = true;
                column.AppearanceCell.TextOptions.HAlignment = hAlignment;
            }
            gridView.EndUpdate();
        }
        public static void DinhDangCot
         (
            DevExpress.XtraGrid.Views.Grid.GridView gridView,
            DevExpress.XtraGrid.Columns.GridColumn columns,
            string formatString,
            DevExpress.Utils.FormatType formatType = DevExpress.Utils.FormatType.Custom
         )
        {
            gridView.Columns[columns.FieldName].DisplayFormat.FormatType = formatType;
            gridView.Columns[columns.FieldName].DisplayFormat.FormatString = formatString;
        }
        public static void CanChuan(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            gridView.BeginUpdate();
            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                gridView.Columns[i].BestFit();
            }
            gridView.EndUpdate();
        }
        public static void TinhToan
            (
                 DevExpress.XtraGrid.Views.Grid.GridView gridView,
                 DevExpress.XtraGrid.Columns.GridColumn column,
                 DevExpress.Data.SummaryItemType KieuTinhToan,
                 string format
            )
        {
            gridView.OptionsView.ShowFooter = true;
            column.SummaryItem.SummaryType = KieuTinhToan;
            column.SummaryItem.DisplayFormat = format;
        }
        public static void ThemDong
        (
            DevExpress.XtraGrid.Views.Grid.GridView gridView,
            DevExpress.XtraGrid.Views.Grid .NewItemRowPosition ViTri = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top
        )
            {
                gridView.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
                gridView.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.True;

                if (gridView.OptionsBehavior.ReadOnly == true) gridView.OptionsBehavior.ReadOnly = false;
                gridView.OptionsBehavior.Editable = true;

                gridView.OptionsView.ShowIndicator = true;
                gridView.OptionsView.NewItemRowPosition = ViTri;
            }
        public static void ReadOnlyColumn
            (
                DevExpress.XtraGrid.Views.Grid.GridView gridView,
                DevExpress.XtraGrid.Columns.GridColumn column
            )
                {
                    column.OptionsColumn.ReadOnly = true;
                }
        public static void CauHinhLuaChon
            (
                DevExpress.XtraGrid.Views.Grid.GridView gridView,   
                DevExpress.XtraGrid.Views .Grid .GridMultiSelectMode LuaChon,
                bool cell, 
                bool row
            )
        {
            gridView.OptionsSelection.MultiSelect = false;
            gridView.OptionsSelection.MultiSelectMode = LuaChon;
            gridView.OptionsSelection.EnableAppearanceFocusedCell = cell;
            gridView.OptionsSelection.EnableAppearanceFocusedRow = row;
            gridView.OptionsSelection.EnableAppearanceHideSelection = false;
        }
    }
    
}
